﻿using Eamon.MeituanDotnetSDK.api.common.request;
using Eamon.MeituanDotnetSDK.exception;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Text;

namespace Eamon.MeituanDotnetSDK.attribute
{
    /// <summary>
    /// 注解处理器
    /// </summary>
    public static class AttributeUtils
    {
        public static ApiMetaInfo GetApiMeta<T>(IMeituanRequest<T> req)
        {
            var metaDO = new ApiMetaInfo();
            var clazz = req.GetType();
            var apiMeta = clazz.GetCustomAttribute<ApiMetaAttribute>();
            if (apiMeta != null)
            {
                metaDO.ApiName = apiMeta.ApiName;
                metaDO.ApiVersion = apiMeta.ApiVersion;
                metaDO.BusinessId = apiMeta.BusinessId;
                metaDO.NeedAuth = apiMeta.NeedAuth;
                metaDO.Path = apiMeta.Path;
            }
            else
            {
                throw new MtSdkException("SDK_GET_APIMETA_ERROR", "获取request元数据异常");
            }
            return metaDO;
        }
    }

    public class ApiMetaInfo
    {
        public string ApiName { get; set; }
        public string ApiVersion { get; set; }
        public int BusinessId { get; set; }
        public bool NeedAuth { get; set; }
        public string Path { get; set; }
    }
}
